/**
 * Created by wenguangjia on 2017/5/31.
 */
(function () {
    'use strict';

    angular
        .module('logcenterApp')
        .controller('logarchiveController', logarchiveController);
    logarchiveController.$inject = ['$scope', 'searchService', 'toaster', 'locations'];

    function logarchiveController($scope, searchService, toaster, locations) {

        var notify = function (type, title, message) {
            toaster.pop(type, title, message);
        };
        $scope.locationId = locations[0].locationId;
        $scope.source = "";

        $scope.locationOptions = {
            dataSource: locations,
            dataTextField: "locationName",
            dataValueField: "locationId"
        };

        searchService.queryAction($scope.locationId).then(function (resp) {
            console.log(resp.data);
            $scope.source = resp.data;
            $scope.counts = [];
            $scope.count = 0;
            var obj = resp.data.actions;
            for (var p in obj) {
                $scope.count++;
                $scope.counts.push(p);
            }
        }, function (error) {
        });

        $scope.query = function () {
            searchService.queryAction($scope.locationId).then(function (resp) {
                console.log(resp.data);
                $scope.source = resp.data;
                $scope.counts = [];
                $scope.count = 0;
                var obj = resp.data.actions;
                for (var p in obj) {
                    $scope.count++;
                    $scope.counts.push(p);
                }
            }, function (error) {
            });
        };


        $scope.win = {
            title: '新增配置',
            modal: true,
            visible: false
        };
        $scope.winOptions = {
            modal: true,
            width: "600",
            height: "320"
        };
        $scope.save = function () {
            var flag = verify();
            if (flag) {
                callServiceSave();
            }
        }
        $scope.insert = function () {
            $scope.data = generrate();
            $scope.win.open();
            $scope.win.left();
        };
        $scope.determain = function () {
            if ($scope.data.description == null || $scope.data.description == "") {
                notify('warn', '提示', 'description不能为空');
            } else if ($scope.data.filters[0].value == null || $scope.data.filters[0].value == "") {
                notify('warn', '提示', 'filters[0].value不能为空');
            } else if ($scope.data.filters[1].unit == null || $scope.data.filters[1].unit == "") {
                notify('warn', '提示', 'filters[1].unit不能为空');
            } else if ($scope.data.filters[1].unit_count == null || $scope.data.filters[1].unit_count == "") {
                notify('warn', '提示', 'filters[1].unit_count不能为空');
            } else {
                $scope.source.actions[$scope.count + 1] = $scope.data;
                callServiceSave();
            }
        };
        $scope.cancel = function () {
            $scope.win.close();
        };
        var index = 0;
        $scope.delete = function (item) {
            index = item;
            $scope.dialog.open();
        };
        $scope.dialogActions = [
            {
                text: '确定', action: function (e) {
                var count = 0;
                var obj = {actions: {}};
                for (var p in $scope.source.actions) {
                    if (p == index) {
                        continue;
                    }
                    count++;
                    obj.actions[count] = $scope.source.actions[p];
                }
                $scope.source = obj;
                callServiceSave();
            }, primary: true
            }, {text: '取消'}
        ];


        function generrate() {
            return {
                action: "delete_indices",
                description: null,
                options: {
                    timeout_override: null,
                    continue_if_exception: false,
                    ignore_empty_list: true,
                    disable_action: false
                },
                filters: [
                    {
                        filtertype: "pattern",
                        kind: "prefix",
                        value: null,
                        exclude: null
                    },
                    {
                        filtertype: "age",
                        source: "name",
                        direction: "older",
                        timestring: "%Y.%m.%d",
                        unit: "days",
                        unit_count: null,
                        exclude: null
                    }
                ]
            };
        }

        function verify() {
            var obj = $scope.source.actions;
            for (var p in obj) {
                if (obj[p].description == null || obj[p].description == "") {
                    notify('warn', '提示', p + '.description不能为空');
                    return false;
                } else if (obj[p].filters[0].value == null || obj[p].filters[0].value == "") {
                    notify('warn', '提示', p + '.filters[0].value不能为空');
                    return false;
                } else if (obj[p].filters[1].unit == null || obj[p].filters[1].unit == "") {
                    notify('warn', '提示', p + '.filters[1].unit不能为空');
                    return false;
                } else if (obj[p].filters[1].unit_count == null || obj[p].filters[1].unit_count == "") {
                    notify('warn', '提示', p + '.filters[1].unit_count不能为空');
                    return false;
                }
                return true;
            }
        }

        function callServiceSave() {
            searchService.saveAction($scope.locationId, $scope.source).then(function (resp) {
                $scope.win.close();
                notify('success', '成功', "保存成功！");
                searchService.queryAction($scope.locationId).then(function (resp) {
                    console.log(resp.data);
                    $scope.source = resp.data;
                    $scope.counts = [];
                    $scope.count = 0;
                    var obj = resp.data.actions;
                    for (var p in obj) {
                        $scope.count++;
                        $scope.counts.push(p);
                    }
                }, function (error) {
                });
            }, function (error) {
                notify('error', '失败', "保存失败！");
            });
        }//调用service
    }


})();
